package mysql

import (
	"gorm.io/gorm"
	"manuscript-service/manurpc/manurpc"
)

type Manuscript struct {
	gorm.Model
	Title   string `gorm:"type:varchar(30);not null;commit:'标题'"`
	Image   string `gorm:"type:varchar(30);not null;commit:'图片'"`
	Like    int64  `gorm:"type:int(11);not null;commit:'点赞量'"`
	ClassId int64  `gorm:"type:int(11);not null;commit:'类别id'"`
}

func (m *Manuscript) ManuAdd(db *gorm.DB) error {
	return db.Debug().Create(m).Error
}

func (m *Manuscript) FindManuByTitle(db *gorm.DB, title string) error {
	return db.Debug().Where("title = ?", title).First(m).Error
}

func ManuList(db *gorm.DB, list []*manurpc.ManuList) ([]*manurpc.ManuList, error) {
	err2 := db.Debug().Model(&Manuscript{}).
		Select("manuscripts.id as manuscript_id",
			"manuscripts.title as title",
			"manuscripts.image as image",
			"manuscripts.like as likes",
			"classes.name as class_name",
		).
		Joins("left join classes on classes.id=manuscripts.class_id").
		Find(&list).Error
	return list, err2
}
